package base.thread;

class Account4 {
	int balance;

	public Account4() {
		this.balance = 0;
		System.out.println("Total money:" + balance);
	}

	public synchronized void withdraw() {
		if (balance == 0) {
			try {
				wait();
			} catch (Exception e) {
			}
		}
		balance = balance - 100;
		System.out.println("withdraw 100success");
		notify();

	}

	public synchronized void deposite() {
		if (balance != 0) {
			try {
				wait();
			} catch (Exception e) {
			}
		}
		balance = balance + 100;
		System.out.println("depostie 100success");
		notify();

	}
}

class WithdrawThread extends Thread {
	Account4 account4;

	public WithdrawThread(Account4 account4) {
		this.account4 = account4;
	}

	public void run() {
		for (int i = 0; i < 5; i++) {
			account4.withdraw();
		}
	}
}

class DepositeThread extends Thread {
	Account4 account4;

	public DepositeThread(Account4 account4) {
		this.account4 = account4;
	}

	public void run() {
		for (int i = 0; i < 5; i++) {
			account4.deposite();
		}
	}
}

public class TestProCon {
	public static void main(String[] args) throws Exception{
		
		Account4 account4 = new Account4();
		
		DepositeThread depositeThread=new DepositeThread(account4);
		WithdrawThread withdrawThread=new WithdrawThread(account4);
		depositeThread.start();
		withdrawThread.start();
		depositeThread.join();
		withdrawThread.join();
		System.out.println("finally the total money:"+account4.balance);
	}
}
